package xm.components.toast
{
	import com.greensock.TweenMax;
	
	import flash.display.Stage;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.effects.Tween;
	
	import xm.core.XMSprite;
	import xm.manager.StageManager;
	
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class Toast extends XMSprite
	{
		
		
		private static var _instance : Toast;
		
		
		/**
		 * @return Toast singleton
		 */		
		public static function getInstance():Toast
		{
			if( _instance == null )
			{
				_instance = new Toast();
			}
			
			return _instance;
		}
		
		
		
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		protected var toastTF : TextField;
		protected var timer : Timer;
		protected var delayTime : Number = 5000;
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function Toast()
		{
			super();
		}
		
		
		
		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
			
		}
		
		
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList(enforce);
			
		}
		
		
		
		public function makeText( message : String , duration : Number = 5000, bgColor : uint = 0x333333 ):void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			toastTF.wordWrap = false;
			
			
			if( timer.running )
			{
				timer.stop();
				timer.reset();
			}
			else
			{
				this.alpha = 0;
				s.addChild( this );
				TweenMax.to( this, 0.5, {alpha:1} );
			}
			
			
			
			toastTF.text = message;
			if( toastTF.textWidth > s.stageWidth - 100 )
			{
				toastTF.wordWrap = true;
				toastTF.width = s.stageWidth - 100;
				this.viewWidth = toastTF.width;
			}
			else
			{
				this.viewWidth = toastTF.textWidth+4;
			}
			
			this.backgroundAlpha = 1;
			this.backgroundColor = bgColor;
			
			this.viewHeight = toastTF.textHeight+4;
			this.invalidateDisplayList();
			
			this.x = (s.stageWidth-this.width)/2;
			this.y = s.stageHeight - this.height - s.stageHeight/10;
			
			
			
			
			
			timer.delay = duration;
			timer.start();
			
		}
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
			
			toastTF = new TextField();
			toastTF.selectable = false;
			toastTF.mouseEnabled = false;
			toastTF.mouseWheelEnabled = false;
			toastTF.autoSize = TextFieldAutoSize.LEFT;
			
			var tf : TextFormat = new TextFormat( "돋움", 18, 0xffffff , true);
			toastTF.defaultTextFormat = tf;
			toastTF.setTextFormat( tf );
			
			addChild( toastTF );
			
			timer = new Timer( delayTime, 1 );
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete );
			
		}
			
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
		}
		
		
		/**
		 * remove
		 */		
		override public function remove():void
		{
			super.remove();
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			super.destroy();
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		protected function onTimerComplete(e:TimerEvent):void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			if( s.contains( this ) )
			{
				TweenMax.to( this, 0.5, {alpha:0, onComplete:onOutComplete } );
			}
		}
		
		
		private function onOutComplete():void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			if( s.contains( this ) )
			{
				s.removeChild( this );
			}
		}
		
		
		
		
		
		
	}
}